<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
//引入七牛云方法
use App\Http\Controllers\Common\CommonController;
use DB;
//use  App\Model\ype;
use  App\Model\Goods;

class GoodsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $goods = DB::table('shop_goods')
        ->join('shop_type','shop_goods.typeid','=','shop_type.id')

        ->select(['shop_goods.id','shop_goods.gname','shop_goods.goods_sales','shop_goods.clicknum','shop_goods.goods_store','shop_type.name','shop_goods.company','shop_goods.time_shelf','shop_goods.time_list','shop_goods.status','shop_goods.pic'])->paginate(10);


        //dd($goods);
        $status=["1"=>"新上市","2"=>"在售","3"=>"下架"];

        return view('Admin/GoodsAdmin/goods',['goods'=>$goods,'status'=>$status]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    //create方法加载商品主图的添加页面
    public function create()
    {
        //
        
        $type = DB::table('shop_type')->select(['id','name','pid','path',])->orderBy(DB::raw('concat(path, id)'))->paginate(100);
       
        return view('Admin/GoodsAdmin/create',['type'=>$type]);
       
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //store方法是处理添加
       $time = date('Y-m-d H:i:s');
        $gname = $request->gname;
        if(!$gname) {
                         return back()->with('errorTip','商品名不能为空');
                    }
        
        $goods_store = $request->goods_store;
        if(!$goods_store) {
                         return back()->with('errorTip','库存量不能为空');
                    }
        $typeid = $request->typeid;
        $company = $request->company;
        if(!$company) {
                         return back()->with('errorTip','厂家不能为空');
                    }
       

        $common = new CommonController;

        $res = $common->uploadToQiNiu($request, 'pic');
        
        $pic = '';
        if ($res) {
            $pic = $res['key'];
            $pic = "http://owy3mobpf.bkt.clouddn.com/".$pic;
        }
        $goods = Goods::insertGetId([
                    'gname' => $gname,
                    'goods_store' => $goods_store,
                    'typeid' => $typeid,
                    'company' => $company,
                    'time_shelf'=>time(),
                    'pic' => $pic
                ]);


        // 把数据加入讯搜
        // $data = [
        //         'id'=>$goods,
        //         'gname'=>$gname,
        //         'pic'=>$pic,
        //         'price'=>5599
        //     ];
        // $search = new Search('lamp29');

        // $search->addDocumentData($data);

            if($goods==true){

            return redirect('/admin/Goods/index')->with('msg','添加成功');
             } else {

            }
                return back()->with('errorTip','添加失败');



    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //edit是拿到编辑的id查询到要编辑的数据
        //dd($id);
        $goods = DB::table('shop_goods')->select(['id','gname','goods_sales','clicknum','goods_store','typeid','company','status','pic'])->where('id',$id)->first();
    //dd($goods->pic);
        //dd($goods->id);

        $type = DB::table('shop_type')->select(['id','name','pid','path',])->orderBy(DB::raw('concat(path, id)'))->get();
        //dd($type);
        $status = $goods->status;


        return view('Admin/GoodsAdmin/edit',['goods'=>$goods,'type'=>$type,'status'=>$status]);

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //update方法是处理修改功能
        //dd($request->pic);
        //dd($request);
                 $gname=$request->input('gname');
                    if(!$gname) {
                         return back()->with('errorTip','商品名不能为空');
                    }

                        $goods_store=$request->input('goods_store');
                         if(!$goods_store) {
                         return back()->with('errorTip','库存量不能为空');
                    }

                    $typeid = $request->typeid;
                    //dd($typeid);

                    $status = $request->status;

                     $company = $request->input('company');
                         if(!$company) {
                         return back()->with('errorTip','厂家不能为空');
                    }

                    $pic = $request->pic;

                    if($pic!==null) {

                    $common = new CommonController;

                    $pic = '';
                    $res = $common->uploadToQiNiu($request, 'pic');
                    if ($res) {
                        $pic = $res['key'];
                        $pic = "http://owy3mobpf.bkt.clouddn.com/".$pic;
                    }




                 $bool = DB::table('shop_goods')
                ->where('id',$id)
                ->update([
                        'gname'=>$gname,
                        'goods_store'=>$goods_store,
                        'typeid'=>$typeid,
                        'status'=>$status,
                        'company'=>$company,
                        'pic'=>$pic
                        ]);
            }else{
                 $bool = DB::table('shop_goods')
                ->where('id',$id)
                ->update([
                        'gname'=>$gname,
                        'goods_store'=>$goods_store,
                        'typeid'=>$typeid,
                        'status'=>$status,
                        'company'=>$company

                        ]);

            }
                //dd($bool);
                // 修改的商品也加入索引
                if($bool!==0) {

                // $search = new Search();
                // $search->updateDocumentData($bool);
                return redirect('/admin/Goods/index')->with('msg','修改成功');

                }
                 return redirect('/admin/Goods/index')->with('errorTip','修改失败，您没做任何修改');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //destroy方法是执行删除功能的
        $bool = DB::table('shop_goods')->where('id',$id)->delete();
        //dd($bool);

                //return back()->with('msg','删除成功');
        if($bool){
            // $search = new Search();
            // $search->delDocumentData($id);
                 return redirect('/admin/Goods/index')->with('msg','删除成功');
             }
         }

    }
